package com.fage.spring.impl;

import com.fage.manager.TransactionManager;
import com.fage.pojo.User;
import com.fage.spring.UserDao;
import com.fage.spring.UserService;

import java.util.List;

/**
 * @author lin
 * @version 1.0
 * @date 2020-08-10 11:17
 * @Description TODO
 */
/**
 * 账户的业务层实现类
 * 事务控制应该都是在业务层
 */
public class UserServiceImpl2 implements UserService {

    private UserDao userDao;

    private TransactionManager txManager;

    @Override
    public User saveUser(User user) {
        return userDao.saveUser(user);
    }

    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        try {
            //1.开启事务
            txManager.beginTransaction();
            //2.执行操作
            List<User> accounts = userDao.findAllUser();
            //3.提交事务
            txManager.commit();
        }catch (Exception e){
            //5.回滚操作
            txManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //6.释放连接
            txManager.release();
        }
    }
}
